# -*- coding: utf-8 -*-
'''
Created on 2009/05/28

@author: coLun
'''

from datetime import *
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class User(db.Model):
    school = db.StringProperty()
    nickname = db.StringProperty()
    email = db.StringProperty()
    CDate = db.DateTimeProperty(auto_now_add = True)
    LDate = db.DateTimeProperty(auto_now = True)

    def getHtml(self):
        # いずれ修正
        return u'%s＠%s' % (self.nickname, self.school)
    def getEncNickname(self):
        # いずれ修正
        return self.nickname
    def getEncSchool(self):
        # いずれ修正
        return self.school
    @staticmethod
    def getUser(email):
        key = 'e<' + email + '>'
        o = User.get_by_key_name(key)
        if o!=None:
            return o
        def txn():
            o = User.get_by_key_name(key)
            if o==None:
                o = User(key_name = key, school=u'匿名高専', nickname=u'匿名', email=email)
                o.put()
            return o
        return db.run_in_transaction(txn)
    @staticmethod
    def getNowUser():
        guser = users.get_current_user()
        if guser!=None:
            return User.getUser(guser.email())
        return None
    @staticmethod
    def setUserProfile(email, nickname, school):
        key = 'e<' + email + '>'
        def txn():
            o = User.get_by_key_name(key)
            if o==None:
                o = User(key_name = key, email = email)
            o.nickname = nickname
            o.school = school
            o.put()
        db.run_in_transaction(txn)

class HtmlViewBase(webapp.RequestHandler):
    @staticmethod
    def getSupportMenu():
        return (u'サポートラウンジ', 'http://pc-2009.colun.net/')
    @staticmethod
    def getEditorMenu():
        return (u'競技問題エディタ', 'http://pc-2009.appspot.com/editor/')
    def printHeader(self, title = '', menu = None):
        self.start_time = datetime.now()
        self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
        self.response.out.write('<html>')
        self.response.out.write('<head>')
        if title!='':
            self.response.out.write('<title>%s</title>' % (title))
        self.response.out.write('</head>')
        self.response.out.write('<body>')
        user = User.getNowUser()
        if user!=None:
            self.user = user
            self.response.out.write('<div align="right">')
            self.response.out.write('%s / <a href="%s">logout</a>' % (user.getHtml(), users.create_logout_url(self.request.url)))
            self.response.out.write('</div>')
        else:
            self.response.out.write('<div align="right">')
            self.response.out.write('<a href="%s">login</a>' % (users.create_login_url(self.request.url)))
            self.response.out.write('</div>')
        if title!='':
            self.response.out.write('<div align="left"><font size="+2" color="#008040">%s</font></div>' % (title))
        if menu!=None:
            self.response.out.write('<div align="left">')
            sp = ""
            for m in menu:
                self.response.out.write('%s[<a href="%s">%s</a>]' % (sp, m[1], m[0]))
                sp = " "
            self.response.out.write('</div>')
        self.response.out.write('<hr />')

    def printFooter(self):
        dt = datetime.now() - self.start_time
        self.response.out.write('<hr />')
        self.response.out.write(u'<div align="right">高専プロコン2009競技練習場</div>')
        self.response.out.write('<!-- %.3fmsec -->' % (dt.seconds * 1000 + dt.microseconds / 1000.0))
        self.response.out.write('</body>')
        self.response.out.write('</html>')
    @staticmethod
    def secToTimeStr(sec):
        m = int(sec / 60)
        sec %= 60
        hour = int(m / 60)
        m %= 60
        str = ''
        if 0<hour:
            str += u'%d時間' % (hour)
        if 0<m:
            str += u'%d分' % (m)
        if 0<sec:
            str += u'%d秒' % (sec)
        if str=='':
            str = u'0秒'
        return str
